package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.IGoldProductRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.gold.domain.GoldProduct;
import cn.wolfcode.luowowo.gold.domain.Specification;
import cn.wolfcode.luowowo.gold.service.IGoldProductDetailService;
import cn.wolfcode.luowowo.gold.service.IGoldProductService;
import cn.wolfcode.luowowo.gold.service.ISpecificationService;
import cn.wolfcode.luowowo.gold.vo.GoldIndexVO;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.GoldProductSearchQuery;
import cn.wolfcode.luowowo.search.service.IGoldProductSearchService;
import cn.wolfcode.luowowo.search.template.GoldProductTemplate;
import cn.wolfcode.luowowo.search.vo.StaticVo;
import cn.wolfcode.luowowo.website.annotation.UserParam;

import com.alibaba.dubbo.config.annotation.Reference;

import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.rmi.PortableRemoteObject;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author 猫教授
 * @date 2019/8/21 18:40
 */
@Controller
@RequestMapping("gold")
public class GoldController {

    @Reference
    private IGoldProductService goldProductService;
    @Reference
    private IGoldProductSearchService goldProductSearchService;
    @Reference
    private ISpecificationService specificationService;
    @Reference
    private IGoldProductDetailService goldProductDetailService;
    @Reference
    private IGoldProductRedisService goldProductRedisService;
    @Reference
    private IDestinationService destinationService;

    /**
     * 给用户提供的查询数组
     *
     * @param content
     * @return
     */
    @RequestMapping("search")
    @ResponseBody
    public String[] search(String content) {
        //调用哪个啥查询出数组哈哈哈
        String[] str = goldProductSearchService.searchArrays(content);
        if (str != null) {
            str[0] = content;
            List<String> asList = Arrays.asList(str);
            ArrayList<String> list = new ArrayList<>();
            for (String s : asList) {
                if (list.contains(s)==false){
                    list.add(s);
                }
            }
            String[] strings = new String[list.size()];
            for (int i = 0; i < list.size(); i++) {
                strings[i]=list.get(i);
            }


            return strings;
        } else {
            return null;
        }


    }

    @RequestMapping("keywordSearch")
    public String keywordSearch(@ModelAttribute("qo") GoldProductSearchQuery qo, Model model) {
        //因为参数有可能为空,所以需要进行判断
        Page<GoldProductTemplate> page = null;
        if (qo.getKeyword() == null) {
            page = goldProductSearchService.selectOrderBy(qo);//进去里面再判断是哪种排序
        } else {

            page = goldProductSearchService.selectByKeyword(qo, "typeName", "productName", "subTitle", "parentTypeName");
        }
        model.addAttribute("page", page);
        return "gold/keywordSearch";
    }


    @RequestMapping("")
    public String index(HttpServletRequest request, @UserParam UserInfo userInfo, Model model) {
        //分类TOP8列表
        List<GoldIndexVO> data = goldProductService.selectTop8ByTypeId();
        model.addAttribute("data", data);
        //每日推荐的4个
        model.addAttribute("PUBLISH", goldProductService.selectpubLish());
        return "gold/goldIndex";
    }
    @RequestMapping("payment")
    public String payment(Long specId, @UserParam UserInfo userInfo, Model model,int productNum) {
       //先去查出来,然后共享到页面

        Specification specification=specificationService.getSpecificationById(specId);
        model.addAttribute("spe",specification);
        model.addAttribute("productNum",productNum);
        model.addAttribute("countPrice",specification.getSpecificationOriginalPrice().multiply(new BigDecimal(productNum+"")));
        model.addAttribute("auPrice",specification.getSpecificationActualPrice().multiply(new BigDecimal(productNum+"")));
        model.addAttribute("countGold",specification.getConvertGoldNum()*productNum);
        model.addAttribute("country",destinationService.selectDestByDeep(1));



        return "gold/payment";
    }


    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public List<Destination> getDestByDeep(int deep, Model model) {

        return destinationService.selectDestByDeep(deep);
    }




    /**
     * 商品详情页

     * @param model
     * @return
     */
    @RequestMapping("goldDetail")
    public String goldProductDetail(Model model,Long productId) {

        GoldProduct pro=goldProductService.getOneById(productId);
        //先查出商品的规格表
        List<Specification> specList=specificationService.selectByProductId(productId);
        specList.sort(new Comparator<Specification>() {
            @Override
            public int compare(Specification o1, Specification o2) {
                return o1.getSpecificationActualPrice().compareTo(o2.getSpecificationActualPrice());
            }
        });
        //List<String> detail=goldProductService.getContent(productId);
        List<String> imglist=goldProductDetailService.findByProductId(productId);
        model.addAttribute("pro",pro);
        model.addAttribute("imglist",imglist);
        model.addAttribute("specList",specList);
        //我们还需要共享那个商品种类排行
        List<GoldProduct> typeHotSale=goldProductRedisService.selectTypeHotSaleByTypeId(pro.getProductType().getId());
        if (typeHotSale.size()<5){
            model.addAttribute("typeHotSale",typeHotSale);
        }else {
            List<GoldProduct> products = typeHotSale.subList(0, 5);
            model.addAttribute("typeHotSale",products);
        }
        //我们需要共享商城的前5热销
        List<GoldProduct> storeHotSaleTop5=goldProductRedisService.selectGoldProductRedisService(RedisKeys.STORE_HOT_SALE.getPrefix());
        model.addAttribute("storeHotSaleTop5",storeHotSaleTop5);
        return "gold/goldDetail";
    }

    @RequestMapping("productType")
    public String productType(@ModelAttribute("qo") GoldProductSearchQuery qo, Model model) {
        //查询分组
        //查询大的分组数量
        List<StaticVo> parentType = goldProductSearchService.selectGroupByParentType("parentTypeId", "parentTypeName");
        //查询小的分组数量
        List<StaticVo> Type = goldProductSearchService.selectGroupByType("typeId", "typeName", qo);
        model.addAttribute("parentType", parentType);
        model.addAttribute("Type", Type);
        Page<GoldProductTemplate> page = null;
        //列表查询
        if (qo.getType() == -1) {
            //查询全部只排序
            page = goldProductSearchService.selectOrderBy(qo);//进去里面再判断是哪种排序
        } else if (qo.getType() == 1) {
            //用大分类查询
            page = goldProductSearchService.selectOrderByParentTypeId(qo);
        } else if (qo.getType() == 2) {
            //以小分类查询
            page = goldProductSearchService.selectOrderByTypeId(qo);
        }
        model.addAttribute("page", page);
        return "gold/productType";
    }

}
